// miniprogram/pages/promoter/promoter.js
var util = require('../../util/util.js');
let keyword = ''
let MAX_LIMIT = 10
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow: false,
    activityList: [],
    isShowList: false
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  onSearch(event) {
    //console.log(event.detail.keyword)
    this.setData({
      activityList: []
    })
    keyword = event.detail.keyword
    console.log(keyword)
    this.getActivityList(0)
  },


  // gohhh(){
  //   wx.cloud.callFunction({
  //     name: 'signUp',
  //     data: {
  //       keyword,
  //       $url: 'activelist',
  //       start: 0,
  //       count: MAX_LIMIT
  //     }
  //   }).then((res) => {
  //     console.log(res)
  //   })
  // },


  getActivityList(start = 0){
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'signUp',
      data: {
        keyword,
        $url: 'list',
        start:start,
        count: MAX_LIMIT
      }
    }).then((res) => {
      console.log(res.result)
      let activityList = res.result
      for (let i = 0, len = activityList.length; i < len; i++) {
        activityList[i].createTime = util.formatTime(new Date(activityList[i].createTime))
        console.log(activityList[i].auto)
        activityList[i].isEnd = util.compareTime(activityList[i].auto)
        console.log(activityList[i].isEnd)
      } 
      console.log(activityList)
      console.log(util.formatTime(new Date()))
      
      this.setData({
        activityList: this.data.activityList.concat(activityList)
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })   
  },



  goSignUp(e){
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: `../signUp-detail/signUp-detail?activityId=${e.currentTarget.dataset.activityid}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._isLogin()
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      activityList: []
    })
    this.getActivityList (0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getActivityList(this.data.activityList.length)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  _isLogin() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              this.setData({
                isShowList: true,
                activityList: []
              })
              this.getActivityList(0)
            }
          })
        }
      }
    })

  },

  onPublish(){
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              //console.log(res)
              this.onLoginSuccess({
                detail: res.userInfo
              })
            }
          })
        } else {
          this.setData({
            modalShow: true
          })
        }
      }
    })
  },

  onLoginSuccess(event) {
    const detail = event.detail
      wx.navigateTo({
        url: `../signUp-edit/signUp-edit?nickName=${detail.nickName}&avatarUrl=${detail.avatarUrl}`,
      })
    

  },

  onLoginFail() {
    wx.showModal({
      title: '授权的用户才能发起报名接龙',
      content: '',
    })
  },
})